package com.zjeav.lingjiao.ui.home.model;

import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.BookRequest;
import com.zjeav.lingjiao.base.response.BooksResponse;
import com.zjeav.lingjiao.base.service.MyBookService;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class HomeModel {
    //我的课程列表
    public void getMyBooks( BookRequest request, final OnMainListener listener) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        MyBookService service = retrofit.create(MyBookService.class);
        service.getMyBooks(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<BooksResponse>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result<BooksResponse> booksResponseResult) {
                        listener.OnMyBooksSuccess(booksResponseResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.OnError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public interface OnMainListener {
        void OnMyBooksSuccess(Result<BooksResponse> result);

        void OnError(Throwable throwable);
    }
}
